// 单例

// .h文件的实现
#define SingletonH(methodName) \
+ (instancetype)shared##methodName; \
+ (void)destroy##methodName;

// .m文件的实现
#if __has_feature(objc_arc) // 是ARC

    #define SingletonM(methodName) \
    static id _instance = nil; \
    + (id)allocWithZone:(struct _NSZone *)zone { \
        if (_instance == nil) { \
            static dispatch_once_t onceToken; \
            dispatch_once(&onceToken, ^{ \
                _instance = [super allocWithZone:zone]; \
            }); \
        } \
        return _instance; \
    } \
    - (id)init { \
        static dispatch_once_t onceToken; \
        dispatch_once(&onceToken, ^{ \
            _instance = [super init]; \
        }); \
        return _instance; \
    } \
    + (void)destroy##methodName { \
        _instance = nil; \
    } \
    + (instancetype)shared##methodName { \
        return [[self alloc] init]; \
    }

#else // 不是ARC

    #define SingletonM(methodName) \
    static id _instance = nil; \
    + (id)allocWithZone:(struct _NSZone *)zone { \
        if (_instance == nil) { \
            static dispatch_once_t onceToken; \
            dispatch_once(&onceToken, ^{ \
                _instance = [super allocWithZone:zone]; \
            }); \
        } \
        return _instance; \
    } \
    - (id)init { \
        static dispatch_once_t onceToken; \
        dispatch_once(&onceToken, ^{ \
            _instance = [super init]; \
        }); \
        return _instance; \
    } \
    + (void)destroy##methodName { \
        _instance = nil; \
    } \
    + (instancetype)shared##methodName { \
        return [[self alloc] init]; \
    } \
    - (oneway void)release { } \
    - (id)retain { \
        return self; \
    } \
    - (NSUInteger)retainCount { \
        return 1; \
    } \
    + (id)copyWithZone:(struct _NSZone *)zone { \
        return _instance; \
    } \
    + (id)mutableCopyWithZone:(struct _NSZone *)zone { \
        return _instance; \
    }

#endif
